home *** CD-ROM | disk | FTP | other *** search
- # include "defs.h"
- # include <CType.h>
-
- /*
- * Gobble Rez statements from the input until end of file or until
- * a statement beginning with "INCLUDE" or "READ" is found. Return the
- * filename from INCLUDE and READ statements; return NULL at end-of-file.
- *
- * Always consume an entire Rez statement, no matter what the statement is.
- */
- char *Rparse (f)
- FILE *f;
- {
- char *s; /* For the return from getword() */
- static char buf[LINELEN]; /* For filenames */
- extern char *getword ();
- extern char *readfname ();
-
- while ((s = getword (f)) != NULL)
- {
- if (strcmp (s, "INCLUDE") == 0)
- {
- /* Filename is next word */
- s = readfname (f);
- if (s == NULL)
- return (NULL);
- strncpy (buf, s, LINELEN);
- while ((s = getword (f)) != NULL && strcmp (s, ";") != 0)
- ;
- return (buf);
- }
- else if (strcmp (s, "READ") == 0)
- {
- /* Filename is word just before ";" */
- while ((s = readfname (f)) != NULL && strcmp (s, ";") != 0)
- {
- strncpy (buf, s, LINELEN);
- }
- if (s == NULL)
- return (NULL);
- else
- return (buf);
- }
- while ((s = getword (f)) != NULL && strcmp (s, ";") != 0)
- ;
- }
- return (NULL);
- }
-
- char *Pparse (in_uses, dollar_u, f)
- bool *in_uses;
- bool *dollar_u;
- FILE *f; /* Input file */
- {
- char *s; /* For return from getword() */
- extern char *do_uses ();
- extern char *getword ();
-
- if (*in_uses)
- {
- s = do_uses (dollar_u, f);
- if (s == NULL)
- *in_uses = FALSE; /* Not necessarily end-of-file */
- else
- return (s);
- }
-
- while ((s = getword (f)) != NULL)
- {
- if (strcmp (s, "{$I") == 0 || strcmp (s, "(*$I") == 0)
- {
- return (readfname (f));
- }
- else if (strcmp (s, "USES") == 0)
- {
- *in_uses = TRUE;
- s = do_uses (dollar_u, f);
- if (s == NULL)
- *in_uses = FALSE; /* Not necessarily end-of-file */
- else
- return (s);
- }
- }
- return (NULL);
- }
-
- /*
- * Return filenames, ignore commas, return NULL at end-of-file or semicolon.
- */
- char *do_uses (dollar_u, f)
- bool *dollar_u;
- FILE *f;
- {
- char *s;
- extern char *getword ();
- extern char *readfname ();
-
- while (!(*dollar_u))
- {
- if ((s = readfname (f)) != NULL) /* A filename */
- {
- strncat (s, ".p", LINELEN);
- return (s);
- }
- if ((s = getword (f)) == NULL) /* End of file*/
- return (NULL);
- if (*s == ';') /* End of USES clause */
- return (NULL);
- if (strcmp (s, "{$U") == 0 || strcmp (s, "(*$U") == 0) /* $U */
- {
- *dollar_u = TRUE;
- return (readfname (f));
- }
- if (strcmp (s, "{$I") == 0 || strcmp (s, "(*$I") == 0)
- return (readfname (f));
- /*
- * Else it's a comma or ordinary comment or something
- * ignorable; loop back for more.
- */
- }
-
- while (TRUE)
- {
- if ((s = getword (f)) == NULL) /* End of file*/
- {
- *dollar_u = FALSE;
- return (NULL);
- }
- if (*s == ';') /* End of USES clause */
- {
- *dollar_u = FALSE;
- return (NULL);
- }
- if (strcmp (s, "{$U") == 0 || strcmp (s, "(*$U") == 0) /* $U */
- return (readfname (f));
- if (strcmp (s, "{$I") == 0 || strcmp (s, "(*$I") == 0)
- return (readfname (f));
- /*
- * Else it's a comma or unit name or ordinary comment or something
- * ignorable; loop back for more.
- */
- }
- }
-
- /*
- * Getword -- a general-purpose tokenizer. Returns the next word from
- * the input. Very naive: punctuation is always a single-character token,
- * except for Pascal strings (*$I {$I (*$U {$U ;
- * alphanumberic strings are always single tokens; all else is ignored.
- */
- char *getword (f)
- FILE *f; /* The input */
- {
- int c;
- int i = 0;
- static char buf[LINELEN];
-
- while ((c = getc (f)) != EOF && !isalnum (c) && !ispunct (c))
- ;
-
- if (ispunct (c))
- {
- buf[i++] = c;
- if (c == '(') /* Ugly code to recognize "(*$I" and "(*$U" */
- {
- c = getc (f);
- if (c == '*')
- {
- buf[i++] = c;
- c = getc (f);
- if (c == '$')
- {
- buf[i++] = c;
- c = getc (f);
- if (c == 'I'|| c == 'U')
- buf[i++] = c;
- else
- {
- ungetc (c, f);
- gobbletill ("*)", f);
- }
- }
- else
- {
- ungetc (c, f);
- gobbletill ("*)", f);
- }
- }
- else
- ungetc (c, f);
- }
- else if (c == '{') /* Ugly code to recognize "{$I" and "{$U" */
- {
- c = getc (f);
- if (c == '$')
- {
- buf[i++] = c;
- c = getc (f);
- if (c == 'I' || c == 'U')
- buf[i++] = c;
- else
- {
- ungetc (c, f);
- gobbletill ("}", f);
- }
- }
- else
- {
- ungetc (c, f);
- gobbletill ("}", f);
- }
- }
- buf[i] = EOS;
- }
- else if (c != EOF)
- {
- while (i < (LINELEN - 1) && isalnum (c))
- {
- buf[i++] = c;
- c = getc (f);
- }
- buf[i] = EOS;
- if (c != EOF)
- ungetc (c, f);
- }
-
- if (i == 0)
- return (NULL);
- return (buf);
- }
-
- /*
- * readfname -- return a filename terminated by whitespace,
- * semicolon, comma, asterisk, or curly braces. Take care that it
- * doesn't begin with { or ( or ,
- */
- char *readfname (f)
- FILE *f; /* The input */
- {
- int c;
- int i = 0;
- static char buf[LINELEN];
-
- while ((c = getc (f)) != EOF && !isalnum (c) && !ispunct (c))
- ;
-
- if (c != EOF && c != '{' && c != '}' && c != '('
- && c != '*' && c != ',' && c != ';')
- {
- do
- {
- buf[i++] = c;
- c = getc (f);
- } while (i < (LINELEN - 1)
- && (c != EOF) && !isspace (c) && (c != ',')
- && (c != ';') && (c != '{') && (c != '}') && (c != '*'));
- buf[i] = EOS;
- if (c != EOF)
- ungetc (c, f);
- }
- else if (c != EOF)
- ungetc (c, f);
-
- if (i == 0)
- return (NULL);
- return (buf);
- }
-
- gobbletill (end, f)
- char *end;
- FILE *f;
- {
- char c;
- int i = 0;
-
- do
- {
- c = getc (f);
- if (c == EOF)
- return;
- else if (c == end[i])
- i++;
- else if (c == end[0])
- i = 1;
- else
- i = 0;
- } while (end[i] != EOS);
- }